import { Component, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Route, Router } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { CompanyService } from './company.service';
import { CompanyModel } from './company.model';

/**
 * Company main component
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Component({
    templateUrl: './company.component.html',
    styleUrls: ['./company.component.scss'],
    providers: [CompanyService]
})
export class CompanyComponent implements OnInit, OnDestroy {
    public routes: Route[];
    public sub: Subscription;
    public companySub: Subscription;
    //public headerSub: Subscription;
    public company: CompanyModel;

    constructor(
        private cdRef: ChangeDetectorRef,
        private route: ActivatedRoute,
        private router: Router,
        private companyService: CompanyService) {
    }

    ngOnInit() {
        window.scrollTo(0, 0);      
        this.sub = this.route.params.subscribe(param => {            
            this.companyService.getCompany(param['id']);
        });

        this.companySub = this.companyService.company.subscribe(data => {            
            this.company = data;
            this.cdRef.detectChanges();            
        });

        /* this.headerSub = this.companyService.header.subscribe(data => {
            this.companyHeaderData = data;
            this.cdRef.detectChanges();
        }); */
    }

    ngOnDestroy() {
        this.cdRef.detach();
        this.sub.unsubscribe();
        this.companySub.unsubscribe();
        //this.headerSub.unsubscribe();
    }
}
